package com.ccx.coachsite.ui;

import com.ccx.coachsite.R;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;

public class AppBaseView extends CommonAppView {

	protected boolean isBusy = false;
	private View loadingView, noDataView;
	private Button btnRefresh, btnSettings;
	private View currentView;

	public AppBaseView(Context paramContext, AttributeSet paramAttributeSet) {
		super(paramContext, paramAttributeSet);
		initView();
	}

	public AppBaseView(Context paramContext) {
		super(paramContext);
		initView();
	}

	public void refreshData() {
		if (isBusy)
			return;
		isBusy = true;
		switchView(loadingView);
		LoadDataTask task = new LoadDataTask();
		task.execute(0);
	}

	protected View getCurrentView() {
		return currentView;
	}

	protected void initView() {
		loadingView = inflate(getContext(), R.layout.common_loading_view, null);
		noDataView = inflate(getContext(), R.layout.common_nodata_view, null);
		addView(loadingView);
		addView(noDataView);
		btnRefresh = (Button) noDataView.findViewById(R.id.btn_nodata_reflash);
		btnSettings = (Button) noDataView
				.findViewById(R.id.btn_nodata_settings);
		btnRefresh.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				refreshData();
			}
		});
		btnSettings.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent("/");
				ComponentName comp = new ComponentName("com.android.settings",
						"com.android.settings.WirelessSettings");
				intent.setComponent(comp);
				intent.setAction("android.intent.action.VIEW");
				getContext().startActivity(intent);
			}
		});
	}

	protected View addChild(int viewId) {
		View view = inflate(getContext(), viewId, null);
		addView(view);
		return view;
	}

	protected boolean onLoadData() {
		return true;
	}

	protected void onRefreshDataSuccess() {

	}

	protected void onRefreshDataFail() {
		switchView(noDataView);
	}

	protected void switchView(View view) {
		for (int i = 0; i < getChildCount(); i++) {
			View v = getChildAt(i);
			if (v.equals(view)) {
				v.setVisibility(VISIBLE);
				currentView = v;
			} else
				v.setVisibility(GONE);
		}
	}

	protected void switchView(int id) {
		for (int i = 0; i < getChildCount(); i++) {
			View view = getChildAt(i);
			if (view.getId() == id) {
				view.setVisibility(View.VISIBLE);
				currentView = view;
			} else
				view.setVisibility(View.GONE);
		}
	}

	protected void showLoading() {
		switchView(loadingView);
	}

	class LoadDataTask extends AsyncTask<Integer, Integer, Boolean> {

		@Override
		protected Boolean doInBackground(Integer... params) {
			try {
				return onLoadData();
			} catch (Exception e) {
				e.printStackTrace();
			}
			return false;
		}

		@Override
		protected void onPostExecute(Boolean result) {
			super.onPostExecute(result);
			isBusy = false;
			if (result)
				onRefreshDataSuccess();
			else
				onRefreshDataFail();
		}
	}
}
